package com.mvu.banana.tools;

/**
 * Class description...
 *
 * @author gantz
 */
public class JavaType {
  public String name;
  public String packageName;
  protected String collectionName;
  protected String elementName;
  public boolean isEntity;
  public boolean isCollection;
  public boolean isEnum;

  public JavaType(String type) {
    int i = type.lastIndexOf(".");
    if (i > -1) {
      name = type.substring(i+1);
      packageName = type.substring(0, i);
    } else {
      name = type;
    }
    elementName = name;
  }

  public JavaType(Class type) {
    packageName = type.getPackage().getName();
    name = type.getSimpleName();
    elementName = name;
    isEnum = type.isEnum();
  }

  public JavaType(JavaClass type) {
    packageName = type.packageName;
    name = type.name;
    elementName = type.name;
  }

  public static JavaType get(Object type) {
    String name;
    if (type instanceof Class) {
      return new JavaType((Class) type);
    } else if (type instanceof JavaType) {
      return (JavaType) type;
    } else if (type instanceof JavaClass) {
      return new JavaType((JavaClass) type);
    } else if (type instanceof String) {
      return new JavaType((String) type);
    } else {
      throw new IllegalStateException("Uknown type: " + type);
    }
  }
}
